<template>
  <div id="SpaceManage">
    <a-flex justify="space-between">
      <h2>空间管理</h2>
      <a-space>
        <a-button type="primary" href="/add_space" target="_blank">创建空间</a-button>
        <a-button type="primary" ghost href="/space_analyze?queryPublic=1" target="_blank">
          分析公共图库
        </a-button>
        <a-button type="primary" ghost href="/space_analyze?queryAll=1" target="_blank">
          分析全部空间
        </a-button>
      </a-space>
    </a-flex>
    <!--搜索框-->
    <a-form layout="inline" :model="searchParam" @finish="doSearch" class="searchForm">
      <a-form-item label="空间名称">
        <a-input v-model:value="searchParam.spaceName" placeholder="请输入空间名称" allowClear />
      </a-form-item>
      <a-form-item label="用户名或昵称">
        <a-input v-model:value="searchParam.username" placeholder="请输入用户名或昵称" allowClear />
      </a-form-item>
      <a-form-item label="空间级别">
        <a-select
          v-model:value="searchParam.spaceLevel"
          placeholder="请选择空间级别"
          :options="SPACE_LEVEL_OPTIONS"
          allow-clear
          style="width: 150px"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <!--表格-->
    <a-table
      :columns="columns"
      :data-source="spaceData"
      bordered
      :pagination="pagination"
      @change="pageSizeChange"
    >
      <template #bodyCell="{ column, record }">
        <!--空间级别-->
        <template v-if="column.dataIndex === 'spaceLevel'">
          {{ getSpaceLevelByLevel(record.spaceLevel)?.name }}
        </template>
        <!--空间使用信息-->
        <template v-if="column.dataIndex === 'spaceUseInfo'">
          <div>大小：{{ formatSize(record.totalSize) }} / {{ formatSize(record.maxSize) }}</div>
          <div>数量：{{ record.totalCount }} / {{ record.maxCount }}</div>
        </template>
        <!--用户信息-->
        <template v-if="column.dataIndex === 'username'">
          {{ record.userVO.username }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space wrap>
            <a-button size="middle" :href="`/space_analyze?spaceId=${record.id}`" target="_blank">
              分析
            </a-button>
            <a-button
              type="link"
              size="middle"
              :href="`/add_space?id=${record.id}`"
              target="_blank"
            >
              编辑
            </a-button>
            <a-button danger size="middle" @click="doDelete(record.id)">删除</a-button>
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { deleteSpaceUsingPost, listSpacePageUsingGet } from '@/api/spaceController.ts'
import { getSpaceLevelByLevel, SPACE_LEVEL_OPTIONS } from '@/constants/space.ts'
import { formatSize } from '@/util'

const columns = [
  {
    title: '空间名称',
    dataIndex: 'spaceName',
  },
  {
    title: '空间级别',
    dataIndex: 'spaceLevel',
  },
  {
    title: '使用情况',
    dataIndex: 'spaceUseInfo',
  },
  {
    title: '所属用户',
    dataIndex: 'username',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]
// 图片列表
const spaceData = ref<API.SpaceVO[]>([])
const total = ref(0)
// 搜索条件
const searchParam = reactive<API.listSpacePageUsingGETParams>({
  pageNum: 1,
  pageSize: 20,
  spaceName: '',
  spaceLevel: undefined,
  userId: '',
})

// 获取图片数据
const querySpacePage = async () => {
  const res = await listSpacePageUsingGet({
    ...searchParam,
  })
  spaceData.value = res.data?.records ?? []
  total.value = res.data.total ?? 0
}
// 分页器
const pageSizeOptions = ref<string[]>(['1', '2', '10', '20', '50'])
const pageSizeChange = (page) => {
  searchParam.pageNum = page.current
  searchParam.pageSize = page.pageSize
  querySpacePage()
}
const pagination = computed(() => {
  return {
    current: searchParam.pageNum,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true, // 显示可改变分页大小
    showTotal: (total: number) => `共${total}条`,
    pageSizeOptions: pageSizeOptions.value,
  }
})
const doSearch = () => {
  // 重置页码
  searchParam.pageNum = 1
  querySpacePage()
}
const doDelete = async (id: string) => {
  if (id) {
    await deleteSpaceUsingPost({ id })
    message.success('操作成功')
    querySpacePage()
  }
}
onMounted(() => {
  querySpacePage()
})
</script>

<style scoped>
.searchForm {
  margin-bottom: 16px;
}
</style>
